<template>
	<view class="live-pusher-container">
<!--		<view class="layui">-->
<!--			<button @click="shot()">拍照</button>-->
<!--		</view>-->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentWebview: null, // 当前webview对象
				pusher: null,  // 视频推流对象
				livePusherOper: null,
        type: 'all', //扫码类型
			}
		},
		onLoad(e) {
			if(e.type){
			  this.type = e.type
      }
      uni.removeStorageSync('isVal')
		},
		created() {
			this.init()
		},
    destroy() {
      this.pusher.close();
    },
		methods: {
			init() {
				this.currentWebview = this.$mp.page.$getAppWebview()
				this.pusher = plus.video.createLivePusher("livePusher", {
					url:'',
					mode: 'FHD',
					top:'0px',
					left:'0px',
					width: '100%',
					height: '100%',
					position: 'absolute'
				});
				this.currentWebview.append(this.pusher) // 
				
				uni.showToast({
					title: '正在打开相机',
					icon: 'loading',
					duration: 10000000
				})
				let t = setTimeout(() => {
					this.pusher.preview()
					this.pusher.switchCamera();
					uni.hideToast()
					
					this.createlivePusherOper()
					clearTimeout(t)
				}, 200)
			},
			
			// 创建可以覆盖在视频(相机)之上的内容
			createlivePusherOper() {
				const windowHeight = uni.getSystemInfoSync().windowHeight
				const statusBarHeight = uni.getSystemInfoSync().statusBarHeight
				this.livePusherOper = plus.webview.create(`/static/livePusherOper.html?type=${this.type}`, 'livePusherOper', {
					background: 'transparent',
					zindex: 999,
					height: (windowHeight + statusBarHeight) + 'px',
					top: `-${statusBarHeight}px`
				})
				this.currentWebview.append(this.livePusherOper)  // 显示webview
				// 监听关闭事件
				this.livePusherOper.addEventListener('close', this.handleHide, false)
			},
			
			handleHide(e) {
        uni.navigateBack({
          delta: 1
        })
			},
		}
	}
</script>

<style>
	.live-pusher-container {
		width: 100%;
		height: 100%;
	}
	.layui {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 99999;
	}
</style>
